package com.nantian.ecm.service.datavalid.controller;

import java.io.File;
import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.nantian.ecm.service.datavalid.util.MD5Util;

@RestController
public class MD5ValidController {
	@Value("${test}")
	private String a;
	
	@RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+"，this is second messge";
    }
	
	@RequestMapping("/a")
	public String a(){
		return a;
	}
	
	/**
	 * 对指定文件进行MD5 校验
	 * @param md5 原文件MD5码
	 * @param fileName 指定文件(包含全路径)
	 */
	@RequestMapping(method=RequestMethod.GET,value="/md5")
	public ModelAndView checkMD5(String fileName,String oldmd5){
		ModelAndView mv=new ModelAndView();
		File f=new File(fileName);
		try {
			String newmd5=MD5Util.getMD5ByFile(f);
			if(oldmd5!=null && oldmd5.equalsIgnoreCase(newmd5)){
				mv.addObject("success", true);
				mv.addObject("md5", newmd5);
			}
			else{
				mv.addObject("success", false);
				mv.addObject("md5", newmd5);
			}
		} catch (FileNotFoundException e) {
			mv.addObject("success", false);
			mv.addObject("error", "File not found!");
		}
		return mv;
	}
}
